package cn.ht.jywrwater.module.ht.controller.admin.villagemeterdevice;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.villagemeterdevice.vo.*;
import cn.ht.jywrwater.module.ht.convert.villagemeterdevice.VillageMeterDeviceConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeterdevice.VillageMeterDeviceDO;
import cn.ht.jywrwater.module.ht.service.villagemeterdevice.VillageMeterDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 大口径流量")
@RestController
@RequestMapping("/ht/village-meter-device")
@Validated
public class VillageMeterDeviceController {

    @Resource
    private VillageMeterDeviceService villageMeterDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建大口径流量")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:create')")
    public CommonResult<Long> createVillageMeterDevice(@Valid @RequestBody VillageMeterDeviceCreateReqVO createReqVO) {
        return success(villageMeterDeviceService.createVillageMeterDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大口径流量")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:update')")
    public CommonResult<Boolean> updateVillageMeterDevice(@Valid @RequestBody VillageMeterDeviceUpdateReqVO updateReqVO) {
        villageMeterDeviceService.updateVillageMeterDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大口径流量")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:delete')")
    public CommonResult<Boolean> deleteVillageMeterDevice(@RequestParam("id") Long id) {
        villageMeterDeviceService.deleteVillageMeterDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大口径流量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:query')")
    public CommonResult<VillageMeterDeviceRespVO> getVillageMeterDevice(@RequestParam("id") Long id) {
        VillageMeterDeviceDO villageMeterDevice = villageMeterDeviceService.getVillageMeterDevice(id);
        return success(VillageMeterDeviceConvert.INSTANCE.convert(villageMeterDevice));
    }

    @GetMapping("/list")
    @Operation(summary = "获得大口径流量列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:query')")
    public CommonResult<List<VillageMeterDeviceRespVO>> getVillageMeterDeviceList(@RequestParam("ids") Collection<Long> ids) {
        List<VillageMeterDeviceDO> list = villageMeterDeviceService.getVillageMeterDeviceList(ids);
        return success(VillageMeterDeviceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大口径流量分页")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:query')")
    public CommonResult<PageResult<VillageMeterDeviceRespVO>> getVillageMeterDevicePage(@Valid VillageMeterDevicePageReqVO pageVO) {
        PageResult<VillageMeterDeviceDO> pageResult = villageMeterDeviceService.getVillageMeterDevicePage(pageVO);
        return success(VillageMeterDeviceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大口径流量 Excel")
    @PreAuthorize("@ss.hasPermission('ht:village-meter-device:export')")
    @OperateLog(type = EXPORT)
    public void exportVillageMeterDeviceExcel(@Valid VillageMeterDeviceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VillageMeterDeviceDO> list = villageMeterDeviceService.getVillageMeterDeviceList(exportReqVO);
        // 导出 Excel
        List<VillageMeterDeviceExcelVO> datas = VillageMeterDeviceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "大口径流量.xls", "数据", VillageMeterDeviceExcelVO.class, datas);
    }

}
